package org.msfj.admin.api;

import org.msfj.admin.feign.UserApi;
import org.msfj.admin.model.User;
import org.msfj.admin.org.service.UserService;
import org.msfj.admin.vo.UserVo;
import org.msfj.admin.feign.UserApi;
import org.msfj.admin.model.User;
import org.msfj.admin.org.service.UserService;
import org.msfj.admin.vo.UserVo;
import org.msfj.admin.feign.UserApi;
import org.msfj.admin.model.User;
import org.msfj.admin.org.service.UserService;
import org.msfj.admin.vo.UserVo;
import org.msfj.common.model.CommonResult;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;

/**
 * @Description:
 * @Author: like
 * @Date: 2017-10-25 14:06
 * @Version: 1.0
 * @Created in idea by autoCode
 */
@RestController
public class UserApiImpl implements UserApi {

    @Autowired
    private UserService userService;

    /**
     * 根据用户id获取用户信息
     *
     * @param id
     * @return
     */
    @Override
    public CommonResult<UserVo> getById(@RequestParam("id") String id) {
        User byId = this.userService.getById(id);
        /*if (byId != null) {
            UserVo userVo = this.userService.setOtherProperties(byId);
            UserVo result = new UserVo();
            BeanUtils.copyProperties(userVo, result);
            return CommonResult.ok(result);
        }
        return CommonResult.error("没有查到");*/
        UserVo userVo = this.userService.setOtherProperties(byId);
        return CommonResult.ok(userVo);
    }

    @Override
    public CommonResult<String> getNameById(String id) {
        User byId = this.userService.getById(id);
        if (byId != null) {
            return CommonResult.ok(byId.getName());
        }
        return CommonResult.ok(null);
    }

    /**
     * 根据id列表查询多个用户的信息
     *
     * @param ids
     * @return
     */
    @Override
    public CommonResult<List<UserVo>> getByIds(@RequestParam(value = "ids",required = false) List<String> ids) {
        if (ids == null || ids.isEmpty()) {
            return CommonResult.ok(Collections.emptyList());
        }
        List<UserVo> byIds = this.userService.getByIds(ids);
        if (byIds != null) {
            List<UserVo> result = new ArrayList<>();
            for (UserVo byId : byIds) {
                UserVo userVo = new UserVo();
                BeanUtils.copyProperties(byId, userVo);
                result.add(userVo);
            }
            return CommonResult.ok(result);
        }
        return CommonResult.ok(Collections.emptyList());
    }

    /**
     * 查询所有用户
     *
     * @return
     */
    @Override
    public CommonResult<List<UserVo>> getAllUser() {
        List<UserVo> allUser = this.userService.getAllUser();
        if (allUser != null) {
            List<UserVo> result = new ArrayList<>();
            for (UserVo byId : allUser) {
                UserVo userVo = new UserVo();
                BeanUtils.copyProperties(byId, userVo);
                result.add(userVo);
            }
            return CommonResult.ok(result);
        }
        return CommonResult.ok(Collections.emptyList());
    }

    /**
     * 查询所有用户
     *
     * @return
     */
    @Override
    public CommonResult<List<UserVo>> getAllUser(@RequestBody UserVo userVo) {
        List<UserVo> allUser = this.userService.getAllUser(userVo);
        if (allUser != null) {
            return CommonResult.ok(allUser);
        }
        return CommonResult.ok(Collections.emptyList());
    }

    @Override
    public CommonResult updateNewPwd(@RequestBody UserVo userVo) {
        userService.saveOrEdit(userVo);
        return CommonResult.ok();
    }

    /**
     * 根据用户id列表查询用户名称
     *
     * @param ids
     * @return
     */
    @Override
    public CommonResult<Map<Long, String>> getUserNames(@RequestParam(value = "ids",required =false) List<String> ids) {
        if (ids == null) {
            return CommonResult.ok(Collections.EMPTY_MAP);
        }
        Map<String, String> userNames = this.userService.getUserNames(ids);
        return CommonResult.ok(userNames);
    }

    @Override
    public CommonResult<UserVo> getByAccount(@RequestParam("account") String account) {
        UserVo userVo = userService.getUserByAccount(account);
        return CommonResult.ok(userVo);
    }
}
